/************************************************************************/
/* GUI                                                                  */
/************************************************************************/
#include "GUI/Panels/MenuPanel.h"

namespace TD_GUI {

	const string MenuPanel::m_menuPanelBackground = "GUI/pause menu.png";
	const string MenuPanel::m_menuPanelBackground2 = "GUI/pause menu2.png";
	const Coord2D MenuPanel::m_menuPanelPosition(0,0);
	const Coord2D MenuPanel::m_menuPanelSize(TD_Game::screenWidth, TD_Game::screenHeight);
	//const Coord2D MenuPanel::m_menuPanelSize(1074, 800);


	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/

	MenuPanel::MenuPanel() : BasePanel(m_menuPanelPosition, m_menuPanelSize) {
		
		m_pImage = m_pMainImg = TD_Resources::ResourceManager::loadImage(m_menuPanelBackground, Color::PINK);
		m_pAuxiliaryImg = TD_Resources::ResourceManager::loadImage(m_menuPanelBackground2, Color::PINK);
		m_auxiliaryMenu = false;

		m_questionIndex = 0;

		m_pQuestionMarkIcon = new Tile(Coord2D(0.3957*TD_Game::screenWidth, 0.375*TD_Game::screenHeight), Coord2D(40, 40), imgTile, TD_Resources::ResourceManager::loadImage("GUI/Icons/question mark icon.png", Color::PINK));
		
		m_pFireSound = TD_Resources::ResourceManager::loadSound("SFX/sounds/fire.wav");

	}

	MenuPanel::~MenuPanel() {
		Utils::safeDelete<Tile>(m_pQuestionMarkIcon);
	}



	void MenuPanel::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		if(isVisible()) {
			BasePanel::render(g_destSurface, deltaTime);
			m_pQuestionMarkIcon->render(g_destSurface, deltaTime);
		}

	}

	void MenuPanel::moveIconDown(unsigned int i) {
		if(m_pQuestionMarkIcon != NULL && m_questionIndex < i) {
			m_pQuestionMarkIcon->setPosition(m_pQuestionMarkIcon->getPosition() + Coord2D(0, 0.14125*TD_Game::screenHeight));
			++m_questionIndex;
			m_pFireSound->playSound(false);
		}
	}

	void MenuPanel::moveIconUp(unsigned int i) {
		if(m_pQuestionMarkIcon != NULL && m_questionIndex > i) {
			m_pQuestionMarkIcon->setPosition(m_pQuestionMarkIcon->getPosition() - Coord2D(0, 0.14125*TD_Game::screenHeight));
			--m_questionIndex;
			m_pFireSound->playSound(false);
		}

	}

	void MenuPanel::setAuxiliaryMenu(bool enable) {
		if(enable) {
			m_auxiliaryMenu = true;
			m_pImage = m_pAuxiliaryImg;
		} else {
			m_auxiliaryMenu = false;
			m_pImage = m_pMainImg;
		}
	}

	interaction_t MenuPanel::processEvent(const SDL_Event &event) {
		if( event.type == SDL_KEYDOWN ) { 
			//Set the proper message surface
			switch( event.key.keysym.sym ) {

				//ESCAPE
				case SDLK_ESCAPE:
					m_interaction.type = hideMenu;
					setAuxiliaryMenu(false);
					return(m_interaction);
				break;

				case SDLK_DOWN:
					moveIconDown(2);
				break;
				
				case SDLK_UP:
					if(!m_auxiliaryMenu)
						moveIconUp(0);
					else
						moveIconUp(1);
				break;

				case SDLK_RETURN:
					if(!m_auxiliaryMenu) {
						switch(m_questionIndex) {
							case 0: //User selected CONTINUE
								m_interaction.type = hideMenu;
								setAuxiliaryMenu(false);
								//Yes, return!
								return(m_interaction);
							break;

							case 1: //User selected RESTART
								m_interaction.type = restartGame;
								setAuxiliaryMenu(true);
							break;

							case 2: //User selected EXIT
								m_interaction.type = gameExit;
								setAuxiliaryMenu(true);
								//Adjusting the question mark icon position 
								//to be the option YES as default
								m_questionIndex = 1;
								m_pQuestionMarkIcon->setPosition(m_pQuestionMarkIcon->getPosition() - Coord2D(0, 113));
							break;
						}
					} else { //We are in the auxiliary menu
						switch(m_questionIndex) {
							case 1: //User selected YES 
								//return the previous interaction
								setAuxiliaryMenu(false);
								return(m_interaction);
							break;

							case 2: //User selected NO
								//Return nothing
								setAuxiliaryMenu(false);
								if(m_interaction.type == restartGame) {
									//Adjusting the question mark icon position 
									//to be the option "restart"
									m_questionIndex = 1;
									m_pQuestionMarkIcon->setPosition(m_pQuestionMarkIcon->getPosition() - Coord2D(0, 113));
								}

								
							break;
						}
							
					}
				break;
			} 
		}
		interaction_t none;
		none.type = noInteraction;
		return(none);
	}
}